<?php

namespace common\corelib\rabbitmq;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use Yii\Base\Exception;

class rabbitmq
{
    public static $conn;
    public $host = 'localhost';
    public $port = '5672';
    public $user = 'guest';
    public $pass = 'guest';
    public $vhost = '/';
    public $connection = '';


    /**
     * rabbitmq constructor.
     * construct
     */
    public function __construct($config = array())
    {
        if(empty($config)) {
            $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->pass, $this->vhost);
        } else {
            $this->connection = new AMQPStreamConnection($config['host'], $config['port'], $config['user'], $config['pass'], $config['vhost']);
        }
        $this->channel = $this->connection->channel();
    }



    /*
     * 申明队列名
        name: $queue
        passive: false
        durable: true // the queue will survive server restarts
        exclusive: false // the queue can be accessed in other channels
        auto_delete: false //the queue won't be deleted once the channel is closed.
    */
    public function queueDeclare($queueName = '', $type = 'direct', $passive = false, $durable = false, $auto_delete = false)
    {
        if(empty($queueName) && !is_string($queueName)) {
            echo new Exception('队列名不能为空!');
            exit;
        }
        $this->channel->queue_declare($queueName, $type, $passive, $durable, $auto_delete);
        return $queueName;
    }



    /*
     * 申明交换机名
        name: $exchange
        type: direct
        passive: false
        durable: true // the exchange will survive server restarts
        auto_delete: false //the exchange won't be deleted once the channel is closed.
    */
    public function exchangeDeclare($exchange = '', $type = 'direct', $passive = false, $durable = false, $auto_delete = false)
    {
        if(empty($exchange) && !is_string($exchange)) {
            echo new Exception('交换机名不能为空!');
            exit;
        }
        $this->channel->exchange_declare($exchange, $type, $passive, $durable, $auto_delete);
        return true;
    }



    /**
     * 绑定队列到交换机
     * bind queue to an exchange
     * @param string    $queue          队列
     * @param string    $exchange       交换机
     * @param string    $routing_key    路由key
     * @param bool      $nowait         是否需要等待
     * @param null      $arguments      可选参数
     * @param null      $ticket
     * @return mixed
     */
    public function queueBind($queue, $exchange, $routing_key = "", $nowait = false, $arguments = null, $ticket = null)
    {
        if(empty($queue) && !is_string($queue)) {
            echo new Exception('队列名不能为空!');
            exit;
        }

        if(empty($exchange) && !is_string($exchange)) {
            echo new Exception('交换机名不能为空!');
            exit;
        }
        $result = $this->channel->queue_bind($queue, $exchange, $routing_key, $nowait, $arguments, $ticket);
        return $result;
    }



    /**
     * 开启一个消费者
     * Starts a queue consumer
     *
     * @param string $queue
     * @param string $consumer_tag
     * @param bool $no_local
     * @param bool $no_ack
     * @param bool $exclusive
     * @param bool $nowait
     * @param callback|null $callback
     * @param int|null $ticket
     * @param array $arguments
     * @return mixed|string
     */
    public function BasicConsumer($queue = '', $consumer_tag = '', $no_local = false, $no_ack = false, $exclusive = false, $nowait = false, $callback = null, $ticket = null, $arguments = array())
    {
        $this->channel->basic_consume($queue, $consumer_tag, $no_local, $no_ack, $exclusive, $nowait, $callback, $ticket, $arguments);
        return true;
    }














}